interface IVec3 {
    x?: number, y?: number, z?: number,
}
export enum PlayerAction {
    next = 'next',                              //点击next跳转商店
    again = 'again',                            //点击again跳转商店
    download = 'download',                      //点击download跳转商店
    automatic_jump = 'automatic_jump',          //自动跳转商店
    select_skill = 'select_skill',              //选择技能跳转商店
    play_now = 'play_now',                      //点击领取跳转商店
}

enum EventName {
    loading = 'loading',                        //loading结束时上报
    game_start = 'game_start',                  //点击开始游戏、拖动人物开始游戏、进入试玩立即开始游戏、点击again且再次游戏时上报
    game_end = 'game_end',                      //每次游戏结束，上报游戏结果
    actionbar = 'actionbar',                    //因为点击按钮、自动跳转等行为导致页面跳转到商店时上报
    interrupt = 'interrupt',                    //因为点击跳转、弹窗跳转、页面切换、页面关闭等行为导致的页面关闭和不可见时，上报
    game_touch = 'game_touch',                  //游戏存在操作角色行为时上报，
    stuck = 'stuck',                            //游戏fps过低上报，
    game_event = 'game_event',                  //触发小兵融合、机关、功能卡、击杀小兵结果选择上报
    stutter = 'stutter',                        //最新fps打点
}

enum AppId {
    LastWar = "LW",
    TopHeros = "TH",
    TopWar = "TW",
}

enum AppName {
    LastWar = "打砖块桥",
    TopHeros = "王国之歌",
    TopWar = "TW",
}

export class PrintComponent {

    //媒体渠道
    private static appid: string = AppId.LastWar;
    private static media: string = "AppLovin";
    private static material: string = AppName.LastWar;

    private static userId: string;

    private static uuid: string;

    //游戏开始时间
    private static startTime: number;

    //总计暂停时间
    private static totalPausedTime: number;

    //游戏开始后暂停时间
    private static gamePausedTime: number;

    //暂停时间戳
    private static lastPauseTime: number;

    //游戏开始时间戳
    private static lastGameStartTime: number;

    //累计游戏次数
    private static totalGamesPlayed: number;


    private static loaded = false;

    private static isPlaying: boolean;

    private static gameStarted: boolean;

    private static stage = 0;

    private static maxStage;

    public static init(maxStage = 1) {
        //初始化
        this.startTime = Date.now();
        this.totalPausedTime = 0;
        this.gamePausedTime = 0;
        this.lastPauseTime = 0;
        this.lastGameStartTime = 0;
        this.totalGamesPlayed = 1;
        this.isPlaying = false;
        this.gameStarted = false;
        this.userId = this.getUserId();
        this.uuid = 'uuid_' + Math.random().toString(36).substr(2, 9)

        this.maxStage = maxStage;

        console.log('Print init');
    }

    public static setMediaAndMaterial(channel: string, version: number){
        this.media = channel;
        this.material = AppName.LastWar + channel.substring(0, 1).toUpperCase() + version;
        console.log('media: ', this.media, '; material: ', this.material);
    }

    private static getUserId() {
        let canvas = window.document.getElementById('GameCanvas')
        return this.hashString(canvas['toDataURL']('image/png')).toString();
    }

    private static hashString(str) {
        let hash = 0;
        for (let char of str) {
            hash = (hash << 5) - hash + char.charCodeAt(0);
        }
        return hash;
    }

    private static getCurrentDuration() {
        return Date.now() - this.startTime - this.totalPausedTime;
    }

    /**
     * 上报事件
     */
    private static reportEvent(eventType: EventName, additionalParams = {}) {
        let params: any = {
            ...window['printParams'],
            'appid': this.appid,
            'media': this.media,
            'material': this.material,
            'user_id': this.userId,
            'uuid': this.uuid,
            ...additionalParams,
            // 'debug_mode': true
        };
        if (!params.duration) {
            params.duration = this.getCurrentDuration();
        }


        //@ts-ignore
        window.gtag && window.gtag('event', eventType, params);
    }

    /**
     * loading结束调用
     */
    public static loading() {
        if (this.loaded) {
            return;
        }
        this.reportEvent(EventName.loading);
        this.loaded = true;
    }

    /**
     * 游戏开始时调用
     */
    public static startGame(stageNum = 1) {
        if (!this.isPlaying) {
            this.isPlaying = true;
            this.gameStarted = true;
            this.gamePausedTime = 0;
            this.lastGameStartTime = Date.now();

            let stage: any = stageNum;
            if (stage == this.maxStage) {
                stage = 'final'
            }
            this.reportEvent(EventName.game_start, { stage, 'total_games_played': this.totalGamesPlayed });
        }
    }

    /**
     * 点击重新开始时调用
     */
    public static replay() {
        ++this.totalGamesPlayed;
    }

    /**
     * 操作主角时调用
     * @param type type = "touch-start" | "touch-end"
     * @param pos 主角位置
     */
    public static game_interaction(type: string) {
        if (!this.isPlaying) {
            return;
        }

        this.reportEvent(EventName.game_touch, {
            type,
        });
    }

    public static game_event(param) {
        this.reportEvent(EventName.game_event, param);
    }

    /**
     * 游戏结束时调用
     */
    public static endGame(isWin, stageNum = 1, level = 1) {
        if (this.isPlaying) {
            this.isPlaying = false;
            let stage: any = stageNum;
            if (stage == this.maxStage) {
                stage = 'final'
            }

            let param: any = {
                stage,
                total_games_played: this.totalGamesPlayed,
                value: isWin ? 'win' : 'lose',
                game_duration: Date.now() - this.lastGameStartTime - this.gamePausedTime,
                type: level
            }

            this.reportEvent(EventName.game_end, param);
        }
    }

    /**
     * 跳转到商店调用
     */
    public static actionbar(action: PlayerAction) {
        this.reportEvent(EventName.actionbar, { value: action });
    }

    public static stutter(param): void {
        this.reportEvent(EventName.stutter, param);
    }
}
